package com.padakeji.android.ui.examples;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.padakeji.android.ui.widget.AdapterViewFlipperX;
import com.padakeji.android.ui.widget.SimpleSlide;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by atanl on 2016-01-03.
 */
public class SimpleSlideTestActivity extends Activity {

    @Bind(R.id.activity_bannerFlipperTest_banner)
    SimpleSlide mBannerFlipper;

    private BaseAdapter mAdapter;

    private String[] imageUrls = {
            "http://img4q.duitang.com/uploads/item/201502/02/20150202215554_zW8um.jpeg",
            "http://img.tvsou.com/NewImages/20120222/20120222172928_0483.jpg",
            "http://imgsrc.baidu.com/zhangbai/pic/item/09fa513d269759ee9c98c771b2fb43166c22df44.jpg",
            "http://imgsrc.baidu.com/baike/pic/item/314e251f95cad1c83c12d76a7f3e6709c83d51d5.jpg",
            "http://np29.yule.com.cn/html/UploadPic/2012-11/2012112614312811313.jpg",
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner_flipper_test);
        ButterKnife.bind(this);

        configViews();

    }

    private void configViews() {
        mAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return imageUrls.length;
            }

            @Override
            public String getItem(int position) {
                return imageUrls[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder vh;
                if (convertView == null) {
                    vh = new ViewHolder();
                    convertView = vh.image = new ImageView(getApplicationContext());
                    vh.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    convertView.setLayoutParams(params);
                    convertView.setTag(vh);
                } else  {
                    vh = (ViewHolder)convertView.getTag();
                }

                String imageUrl = getItem(position);
                Picasso.with(getApplicationContext()).load(imageUrl).into(vh.image);
                return convertView;
            }

        };

        mBannerFlipper.setAdapter(mAdapter);
        mBannerFlipper.setSelection(2);

//        mBannerFlipper.setAutoStart(true);
        mBannerFlipper.setFlipInterval(4000);
    }

    private class ViewHolder {
        ImageView image;
    }


}
